import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';
import {
    InitData,
    WorkItems,
    WorkItemDetail,
    Categories,
    Part,
    ProductCategory,
    WPC,
} from './DTO';
const baseUrl = '/afterSales/api/v1';
import {workItemCategoryStatus, productCategoryType} from '../Enum';
const resourceName = formatMessage({
    id: 'api.resourceName',
    defaultMessage: '维修项目'
});

const listDesc = formatMessage({
    id: 'api.message.listDesc',
    defaultMessage: '查询{resourceName}'
}, {resourceName});

const detailDesc = formatMessage({
    id: 'api.message.detailDesc',
    defaultMessage: '查询{resourceName}详情'
}, {resourceName});

const addDesc = formatMessage({
    id: 'api.message.addDesc',
    defaultMessage: '新增{resourceName}'
}, {resourceName});

const updateDesc = formatMessage({
    id: 'api.message.updateDesc',
    defaultMessage: '编辑{resourceName}'
}, {resourceName});

const disableDesc = formatMessage({
    id: 'api.message.disableDesc',
    defaultMessage: '停用{resourceName}'
}, {resourceName});

const enableDesc = formatMessage({
    id: 'api.message.enableDesc',
    defaultMessage: '启用{resourceName}'
}, {resourceName});

const exportDesc = formatMessage({
    id: 'api.exportDesc',
    defaultMessage: '导出{resourceName}'
}, {resourceName});

const updateLaborhourDesc = formatMessage({
    id: 'api.message.updateLaborhourDesc',
    defaultMessage: '编辑项目工时'
});

const resourceName2 = formatMessage({
    id: 'api.resourceName2',
    defaultMessage: '维修项目分类'
});

const categoryDesc = formatMessage({
    id: 'api.message.categoryDesc',
    defaultMessage: '查询{resourceName2}'
}, {resourceName2});

const addCategoryDesc = formatMessage({
    id: 'api.message.addCategoryDesc',
    defaultMessage: '新增{resourceName2}'
}, {resourceName2});

const updateCategoryDesc = formatMessage({
    id: 'api.message.updateCategoryDesc',
    defaultMessage: '编辑{resourceName2}'
}, {resourceName2});

const disableCategoryDesc = formatMessage({
    id: 'api.message.disableCategoryDesc',
    defaultMessage: '停用{resourceName2}'
}, {resourceName2});

const enableCategoryDesc = formatMessage({
    id: 'api.message.enableCategoryDesc',
    defaultMessage: '启用{resourceName2}'
}, {resourceName2});


const resourceName3 = formatMessage({
    id: 'api.resourceName3',
    defaultMessage: '维修模式'
});

const modeDesc = formatMessage({
    id: 'api.message.modeDesc',
    defaultMessage: '查询{resourceName3}'
}, {resourceName3});


const addModeDesc = formatMessage({
    id: 'api.message.addModeDesc',
    defaultMessage: '新增{resourceName3}'
}, {resourceName3});

const updateModeDesc = formatMessage({
    id: 'api.message.updateModeDesc',
    defaultMessage: '编辑{resourceName3}'
}, {resourceName3});

const exportModeDesc = formatMessage({
    id: 'api.message.exportModeDesc',
    defaultMessage: '导出{resourceName3}'
}, {resourceName3});

const abandonModeDesc = formatMessage({
    id: 'api.message.abandonModeDesc',
    defaultMessage: '作废{resourceName3}'
}, {resourceName3});

const assistDesc = formatMessage({
    id: 'api.message.assistDesc',
    defaultMessage: '编辑辅助维修项目'
});

const partDesc = formatMessage({
    id: 'api.message.partDesc',
    defaultMessage: '查询主机厂配件'
});
const productCategoryDesc = formatMessage({
    id: 'api.message.productCategoryDesc',
    defaultMessage: '查询车型'
});

// 初始化查询
export const initQuery = () => request(`${baseUrl}/ui/workItem/init`, null, InitData)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, initQuery));
// 查询
export const getWorkItems = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/workItems/forCurrentUser?${queryString}`, null, WorkItems)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, listDesc));
};

// 查询详情
export const getWorkItemDetail = id => request(`${baseUrl}/workItems/${id}`, null, WorkItemDetail)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, detailDesc));

// 新增
export const addWorkItem = data => request(`${baseUrl}/workItems`, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, addDesc));

// 编辑
export const updateWorkItem = (id, data) => request(`${baseUrl}/workItems/${id}`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateDesc));

// 停用
export const disableWorkItem = (id, data) => request(`${baseUrl}/workItems/${id}/disable`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, disableDesc));

// 启用
export const enableWorkItem = (id, data) => request(`${baseUrl}/workItems/${id}/enable`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, enableDesc));

// 导出
export const exportWorkItem = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/workItems/export/forCurrentUser?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportDesc))
        .then(res => res.data);
};
// 更新维修项目的工时信息
export const updateLaborhour = (id, data) => request(`${baseUrl}/workItems/${id}/laborHour`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateLaborhourDesc));

// 更新维修项目的工时信息
export const updateAssist = (id, data) => request(`${baseUrl}/workItems/${id}/assistItems`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, assistDesc));

// 查询顶层维修分类
export const getCategoriesTop = () => request(`${baseUrl}/workItemCategories/topLevel/forCurrentUser`, null, Categories)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, categoryDesc));

// 查询维修分类
export const getCategories = id => request(`${baseUrl}/workItemCategories/${id}/children`, null, Categories)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, categoryDesc));


// 新增维修分类
export const addCategory = data => request(`${baseUrl}/workItemCategories`, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, addCategoryDesc));

// 编辑维修分类
export const updateCategory = (id, data) => request(`${baseUrl}/workItemCategories/${id}`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateCategoryDesc));

// 停用维修分类
export const disableCategory = (id, data) => request(`${baseUrl}/workItemCategories/${id}/disable`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, disableCategoryDesc));

// 启用维修分类
export const enableCategory = (id, data) => request(`${baseUrl}/workItemCategories/${id}/enable`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, enableCategoryDesc));

// 查询维修模式
export const getModes = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/repairModes?${queryString}`, null, WorkItems)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, modeDesc));
};

// 新增维修模式
export const addMode = data => request(`${baseUrl}/repairModes`, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, addModeDesc));

// 编辑维修模式
export const updateMode = (id, data) => request(`${baseUrl}/repairModes/${id}`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateModeDesc));

// 作废维修模式
export const abandonMode = (id, data) => request(`${baseUrl}/repairModes/${id}/abandon`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonModeDesc));

// 导出维修模式
export const exportMode = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/repairModes/export?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportModeDesc))
        .then(res => res.data);
};

// 查询有效的维修项目及工时
export const getWPC = queryCondition => {
    const queryString = stringify({
        ...queryCondition,
        status: workItemCategoryStatus.生效
    });
    return request(`${baseUrl}/workItemProductCategories/forCurrentUser?${queryString}`, null, WPC)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, listDesc));
};


// 导出维修项目及工时
export const exportWPC = queryCondition => {
    const queryString = stringify({
        ...queryCondition,
        status: workItemCategoryStatus.生效
    });
    return request(`${baseUrl}/workItemProductCategories/export?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportDesc))
        .then(res => res.data);
};


// 查询主机厂配件
export const getParts = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/parts/forWorkItem?${queryString}`, null, Part)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, partDesc));
};


// 查询产品分类
export const getPCs = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/productCategories?${queryString}`, null, ProductCategory)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, productCategoryDesc));
};

// 快速查询产品分类
export const getPCSimple = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/productCategories/simple?${queryString}`, null, ProductCategory)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, productCategoryDesc))
        .then(res => {
            if(res.ok) return res.data.content;
            return false;
        });
};

const productCategoryQueryDesc = formatMessage({
    id: 'api.message.productCategoryQueryDesc',
    defaultMessage: '查询车型信息'
});
export const productCategoryQuery = () =>
    request(`${baseUrl}/productCategories?type=${productCategoryType.车型}&isAll=true`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, productCategoryQueryDesc));
